package com.dididoctor.patient.Activity.Doctor;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.TextView;

import com.dididoctor.patient.Activity.Doctor.Doctordetail.DoctorDetailActivity;
import com.dididoctor.patient.MV.adapter.CommonAdapter;
import com.dididoctor.patient.MV.adapter.ViewHolder;
import com.dididoctor.patient.R;
import com.dididoctor.patient.Utils.LoaderImage;
import com.dididoctor.patient.Utils.UserHeadImage;
import com.dididoctor.patient.Utils.Util;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.util.List;

/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： DoctorListAdapter
 * @包名称： com.dididoctor.patient.Activity.Doctor
 * @创建人： zhanjunjie
 * @创建时间：2016/4/7 17:16
 * @修改人：
 * @修改时间：2016/4/7 17:16
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class DoctorListAdapter extends CommonAdapter<Doctor> {
    private TextView mTvNameOrJob, mTvSige, mTvAddress, mTvPing, mTvDistance, mTvdoctor, mTvcall;
    private UserHeadImage headImage;
    private UserHeadImage isonlie;
    private LoaderImage loaderImage;
    private Context context;
    private String longitude, latitude;

    public DoctorListAdapter(Context context, List<Doctor> datas, String longitude, String latitude) {
        super(context, datas, R.layout.item_doctor);
        this.context = context;
        this.longitude = longitude;
        this.latitude = latitude;
        loaderImage = new LoaderImage(context);
    }

    @Override
    public void convert(int position, ViewHolder helper, final Doctor item) {
        isonlie = helper.getView(R.id.img_isonlie);
        headImage = helper.getView(R.id.img_userimg);
        loaderImage.load(headImage, item.getHeadPic(), new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String s, View view) {
                headImage.setImageResource(R.drawable.img_doctordefault);
            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {
                headImage.setImageResource(R.drawable.img_doctordefault);
            }

            @Override
            public void onLoadingComplete(String s, View view, Bitmap bitmap) {

            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }
        });
        if ("1".equals(item.getOnline())) {
            isonlie.setVisibility(View.GONE);
        } else {
            isonlie.setVisibility(View.VISIBLE);
        }
        mTvNameOrJob = helper.getView(R.id.tv_doctornameorjob);
        mTvNameOrJob.setText(item.getName() + "     " + item.getPosition());
        mTvSige = helper.getView(R.id.tv_doctorsigenum);
        String num = Util.isEmpty(item.getSignNum()) ? "0" : item.getSignNum();
        mTvSige.setText("签约数：" + num);
        mTvAddress = helper.getView(R.id.tv_address);
        mTvAddress.setText(item.getHospital());
        mTvPing = helper.getView(R.id.tv_ping);
        if (!"".equals(item.getEvaluate()) && item.getEvaluate() != null) {
            mTvPing.setText("好评率：" + item.getEvaluate() + "%");
        } else {
            mTvPing.setText("好评率：" + "100" + "%");
        }


        mTvDistance = helper.getView(R.id.tv_distance);
        mTvDistance.setText(item.getDistance() + "m");
        mTvdoctor = helper.getView(R.id.tv_isdoctor);
        mTvcall = helper.getView(R.id.tv_call);
        if ("1".equals(item.getCallSwitch())) {//有电话
            mTvcall.setVisibility(View.VISIBLE);
        } else {
            mTvcall.setVisibility(View.GONE);
        }

        if ("1".equals(item.getIsSign())) {//是家庭医生
            mTvdoctor.setVisibility(View.VISIBLE);
        } else {
            mTvdoctor.setVisibility(View.GONE);
        }

        helper.getView(R.id.lin_all).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, DoctorDetailActivity.class);
                intent.putExtra("hospitalId", item.getHospitalId());
                intent.putExtra("longitude", longitude);
                intent.putExtra("latitude", latitude);
                context.startActivity(intent);
            }
        });
    }
}
